package tech.daoxuan.web.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 这样写@component所有的都会过滤
 */
//@Component
public class TimeFilter implements Filter {
    Logger logger = LoggerFactory.getLogger(TimeFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        logger.info("my time filter is init ....");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        logger.info("time filter start -----");

        int nano = LocalDateTime.now().getNano();
        filterChain.doFilter(servletRequest,servletResponse);
        int nano1 = LocalDateTime.now().getNano();

        logger.info("time spend"+ (nano1-nano));
        logger.info("time filter finish ----");

    }

    @Override
    public void destroy() {
        logger.info("time filter destroy .....");
    }
}
